package cn.openea.perpartner.meily.web.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.web.entity.Role;
import cn.openea.perpartner.meily.web.entity.User;
import cn.openea.perpartner.meily.web.entity.vo.UserVo;
import cn.openea.perpartner.meily.web.service.IRoleService;
import cn.openea.perpartner.meily.web.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Weird077
 * @description TODO
 * @date 2023-03-15 23:38
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IRoleService roleService;

    @ApiOperation(value = "用户列表")
    @PostMapping(value = "/list")
    public TableDataInfo list(@RequestBody User user) {
        startPage();
        List<User> list = userService.selectUserlist(user);
        return getDataTable(list);
    }

    @ApiOperation("获取所有用户列表")
    @GetMapping("/all/list")
    public AjaxResult allList(User user){
        List<User> list = userService.selectUserlist(user);
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "用户详情")
    @GetMapping("/info/{userId}")
    public AjaxResult getById(@PathVariable String userId) {
        User userInfo = userService.getById(userId);
        List<Role> roles = roleService.selectRolesByUserId(userId);
        return AjaxResult.success().put("userInfo", userInfo).put("roles", roles);
    }

    @ApiOperation(value = "编辑用户")
    @PostMapping("/edit")
    public AjaxResult updateUser(@RequestBody User user) {

        return toAjax( userService.updateUser(user));
    }
    @ApiOperation(value = "新增用户")
    @PostMapping("/add")
    public AjaxResult insertUser(@RequestBody User user) {
        return toAjax( userService.insertUser(user));
    }

    @ApiOperation(value = "用户授权")
    @PostMapping("/authRole")
    public AjaxResult insertAuthRole(@RequestBody User user) {
        userService.insertUserAuth(user.getUserId(), user.getRoleIds());
        return success();
    }

    @ApiOperation(value = "编辑员工管理详情")
    @PostMapping("/edit/staff")
    public AjaxResult updateStaff(@RequestBody UserVo userVo) {
        System.out.println("==========================");
        System.out.println(userVo);
        System.out.println("==========================");
        return toAjax( userService.updateStaff(userVo));
    }

    @ApiOperation(value = "获取钱包余额")
    @PostMapping("/getCoins/staff")
    public AjaxResult getCoins(@RequestBody UserVo userVo) {
        String Coins = userService.getCoins(userVo);
        if(StrUtil.isNotBlank(Coins)){
            return success(Coins);
        } else {
            return error();
        }
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("/info/staff/{userId}")
    public AjaxResult getUserInfoById(@PathVariable String userId) {
        User existUser = userService.getById(userId);
        if(existUser != null){
            return AjaxResult.error("用户已存在！");
        }
        User userInfo = userService.getUserInfoById(userId);
        if(userInfo == null){
            return AjaxResult.error("此用户不存在！");
        }
        return AjaxResult.success().put("userInfo", userInfo);
    }

    @ApiOperation(value = "删除用户")
    @PostMapping("/delete/staff")
    public AjaxResult deleteUsers(@RequestBody List<String> userIdList) {
        return toAjax(userService.deleteUsers(userIdList));
    }

    @ApiOperation(value = "用户是否有支付特权")
    @GetMapping("/canQuickPay/{userId}")
    public AjaxResult canQuickPay(@PathVariable String userId) {
        String status = "no";// 是否有支付特权。yes:是；no:否。默认：no
        User userInfo = userService.getById(userId);
        if(userInfo != null){
            if("1".equals(userInfo.getQuickPay())){
                status = "yes";
            }
        }
        return AjaxResult.success("用户是否有支付特权", status);
    }

    @ApiOperation(value = "用户是否可以提现")
    @GetMapping("/canCashOut/{userId}")
    public AjaxResult canCashOut(@PathVariable String userId) {
        boolean flag = true;
        User userInfo = userService.getById(userId);
        if(userInfo != null){
            if("1".equals(userInfo.getCashOutLimit())){// 提现限制。0:关闭；1:开启
                flag = false;
            }
        }
        return AjaxResult.success("用户是否可以提现", flag);
    }

}
